<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增收费</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<body>
<div id="aaa">
    <form class="layui-form" action="">
        <input type="hidden" id="staffid" name="staffid" class="input-text">
        <div class="layui-form-item layui-col-xs10">
            <label class="layui-form-label">缴费人</label>
            <div class="layui-input-block">
                <input type="text" id="namebh" name="namebh" lay-verify="required" placeholder="请输入员工名字或编号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item  layui-col-xs10">
            <label class="layui-form-label">金额</label>
            <div class="layui-input-block">
                <input type="text" name="money" id="money" lay-verify="required|double" placeholder="请输入金额" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-inline layui-col-xs10" style="margin-bottom: 15px;">
            <label class="layui-form-label">时间段</label>
            <div class="layui-input-inline">
                <input type="text" id="start_time" name="start_time" lay-verify="required" autocomplete="off" placeholder="请输入开始时间" class="layui-input">
            </div>
            <div style="display: inline;">至</div>
            <div class="layui-input-inline">
                <input type="text" id="end_time" name="end_time" lay-verify="required" autocomplete="off" placeholder="请输入结束时间" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px;">
            <div class="layui-input-block">
                <button class="layui-btn" id="addWuye" lay-submit lay-filter="addWuye">立即缴费</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>

<script>
    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(window.location.search);
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }

    window.onload = function(){
        $("input[name='staffid']").val(GetQueryString("id"));
        // console.log("员工ID："+GetQueryString("id"));
        $("input[name='namebh']").val(GetQueryString("name"));
        $("input[name='start_time']").val(GetQueryString("dqtime"));
    }

    layui.use(['form' , 'jquery'], function(){
        var form = layui.form;
        var $ = layui.jquery;

        //自定义验证规则  检测是否为数字
        form.verify({
            double: function(value){
                if(/^\d+$/.test(value)==false && /^\d+\.\d+$/.test(value)==false)
                {
                    return '你输入的不是数字';
                }
            }
        });

        //添加操作
        form.on('submit(addWuye)',function(data){
            var start_time = $("#start_time").val();
            var end_time = $("#end_time").val();
            if($("#money").val()==0){
                layer.msg("金额不能为0！");
                return false;
            }
            if(start_time > end_time){
                layer.msg("请选择正确时间！");
                return false;
            }else{
                data.field.staffID = $("#staffid").val();
                data.field.ksTime = start_time;
                data.field.jsTime = end_time;
                $.ajax({
                    url:"/shoufei/addshoufei"
                    ,type:"POST"
                    ,contentType: 'application/json'
                    ,data:JSON.stringify(data.field)
                    ,success:function (data) {
                        if(data.code==2){
                            layer.msg(data.data);
                        }else{
                            if(data.code==0){
                                layer.close(layer.index);
                                layer.msg('缴费成功');
                                setTimeout(function(){
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);//关闭弹出层
                                    parent.location.reload();//重新加载父页面表格
                                }, 2000);
                            }else if(code==401){
                                layer.msg("拒绝访问");
                            }else{
                                layer.msg('缴费失败');
                            }
                        }
                    }
                    ,error:function () {
                        console.log("ajax error");
                    }
                });
                return false;
            }
        });
    });
</script>
<script>
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        var endDate= laydate.render({
            elem: '#end_time',//选择器结束时间
            type: 'datetime',
            min:"1970-1-1",//设置min默认最小值
            done: function(value,date){
                startDate.config.max={
                    year:date.year,
                    month:date.month-1,//关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds : 0
                }
            }
        });

        //日期范围
        var startDate=laydate.render({
            elem: '#start_time',
            type: 'datetime',
            max:"2099-12-31",//设置一个默认最大值
            done: function(value, date){
                endDate.config.min ={
                    year:date.year,
                    month:date.month-1, //关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds : 0
                };
            }
        });
    });
</script>
</body>
</html>